package com.mbyte.easy.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbyte.easy.admin.entity.Equapply;
import com.mbyte.easy.admin.entity.Equipment;
import com.mbyte.easy.admin.mapper.EquapplyMapper;
import com.mbyte.easy.admin.mapper.EquipmentMapper;
import com.mbyte.easy.admin.service.IEquapplyService;
import com.mbyte.easy.admin.service.IEquipmentService;
import com.mbyte.easy.common.controller.BaseController;
import com.mbyte.easy.common.web.AjaxResult;
import com.mbyte.easy.security.entity.SysUser;
import com.mbyte.easy.security.mapper.SysUserMapper;
import com.mbyte.easy.util.PageInfo;
import com.mbyte.easy.util.Utility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.ObjectUtils;
import java.util.List;

/**
* <p>
* 前端控制器
* </p>
* @author lp
* @since 2019-03-11
*/
@Controller
@RequestMapping("/equipment/equipment")
public class EquipmentController extends BaseController  {

    private String prefix = "equipment/equipment/";

    @Autowired
    private IEquipmentService equipmentService;
    @Autowired
    private IEquapplyService equapplyService;
    @Autowired
    EquapplyMapper equapplyMapper;
    @Autowired
    SysUserMapper sysUserMapper;



    /**
    * 查询列表
    *
    * @param model
    * @param pageNo
    * @param pageSize
    * @param equipment
    * @return
    */
    @RequestMapping
    public String index(Model model,@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, Equipment equipment) {
        Page<Equipment> page = new Page<Equipment>(pageNo, pageSize);
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<Equipment>();
        if(!ObjectUtils.isEmpty(equipment.getEquId())) {
            queryWrapper = queryWrapper.like("equ_id",equipment.getEquId());
         }
        if(!ObjectUtils.isEmpty(equipment.getEquName())) {
            queryWrapper = queryWrapper.like("equ_name",equipment.getEquName());
         }
        if(!ObjectUtils.isEmpty(equipment.getEquType())) {
            queryWrapper = queryWrapper.like("equ_type",equipment.getEquType());
         }
        if(!ObjectUtils.isEmpty(equipment.getEquSizes())) {
            queryWrapper = queryWrapper.like("equ_sizes",equipment.getEquSizes());
         }
        if(!ObjectUtils.isEmpty(equipment.getEquStatus())) {
            queryWrapper = queryWrapper.like("equ_status",equipment.getEquStatus());
         }
        if(!ObjectUtils.isEmpty(equipment.getEquCreatetime())) {
            queryWrapper = queryWrapper.like("equ_createtime",equipment.getEquCreatetime());
         }
        if(!ObjectUtils.isEmpty(equipment.getEquSave())) {
            queryWrapper = queryWrapper.like("equ_save",equipment.getEquSave());
         }
        if(!ObjectUtils.isEmpty(equipment.getEquSupplier())) {
            queryWrapper = queryWrapper.like("equ_supplier",equipment.getEquSupplier());
         }
        if(!ObjectUtils.isEmpty(equipment.getEquRemaker())) {
            queryWrapper = queryWrapper.like("equ_remaker",equipment.getEquRemaker());
         }
        IPage<Equipment> pageInfo = equipmentService.page(page, queryWrapper);
        model.addAttribute("searchInfo", equipment);
        model.addAttribute("pageInfo", new PageInfo(pageInfo));
        return prefix+"list";
    }

    /**
    * 添加跳转页面
    * @return
    */
    @GetMapping("addBefore")
    public String addBefore(){
        return prefix+"add";
    }
    /**
    * 添加
    * @param equipment
    * @return
    */
    @PostMapping("add")
    @ResponseBody
    public AjaxResult add(Equipment equipment){
        return toAjax(equipmentService.save(equipment));
    }
    /**
    * 添加跳转页面
    * @return
    */
    @GetMapping("editBefore/{id}")
    public String editBefore(Model model,@PathVariable("id")Long id){
        model.addAttribute("equipment",equipmentService.getById(id));
        return prefix+"edit";
    }
    /**
    * 添加
    * @param equipment
    * @return
    */
    @PostMapping("edit")
    @ResponseBody
    public AjaxResult edit(Equipment equipment){
        return toAjax(equipmentService.updateById(equipment));
    }
    /**
    * 删除
    * @param id
    * @return
    */
    @GetMapping("delete/{id}")
    @ResponseBody
    public AjaxResult delete(@PathVariable("id") Long id){
        return toAjax(equipmentService.removeById(id));
    }
    /**
    * 批量删除
    * @param ids
    * @return
    */
    @PostMapping("deleteAll")
    @ResponseBody
    public AjaxResult deleteAll(@RequestBody List<Long> ids){
        return toAjax(equipmentService.removeByIds(ids));
    }



    /**
     * 设备申请  --- 个人用户
     *
     * @param model
     * @param pageNo
     * @param pageSize
     * @param equipment
     * @return
     */
    @RequestMapping("applyEquipment")
    public String applyEquipment(Model model,@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, Equipment equipment) {
        Page<Equipment> page = new Page<Equipment>(pageNo, pageSize);
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<Equipment>();
        if(!ObjectUtils.isEmpty(equipment.getEquId())) {
            queryWrapper = queryWrapper.like("equ_id",equipment.getEquId());
        }
        if(!ObjectUtils.isEmpty(equipment.getEquName())) {
            queryWrapper = queryWrapper.like("equ_name",equipment.getEquName());
        }
        if(!ObjectUtils.isEmpty(equipment.getEquType())) {
            queryWrapper = queryWrapper.like("equ_type",equipment.getEquType());
        }
        if(!ObjectUtils.isEmpty(equipment.getEquSizes())) {
            queryWrapper = queryWrapper.like("equ_sizes",equipment.getEquSizes());
        }
        if(!ObjectUtils.isEmpty(equipment.getEquStatus())) {
            queryWrapper = queryWrapper.like("equ_status",equipment.getEquStatus());
        }
        if(!ObjectUtils.isEmpty(equipment.getEquCreatetime())) {
            queryWrapper = queryWrapper.like("equ_createtime",equipment.getEquCreatetime());
        }
        if(!ObjectUtils.isEmpty(equipment.getEquSave())) {
            queryWrapper = queryWrapper.like("equ_save",equipment.getEquSave());
        }
        if(!ObjectUtils.isEmpty(equipment.getEquSupplier())) {
            queryWrapper = queryWrapper.like("equ_supplier",equipment.getEquSupplier());
        }
        if(!ObjectUtils.isEmpty(equipment.getEquRemaker())) {
            queryWrapper = queryWrapper.like("equ_remaker",equipment.getEquRemaker());
        }
        queryWrapper.eq("equ_status", "空闲");

        IPage<Equipment> pageInfo = equipmentService.page(page, queryWrapper);
        model.addAttribute("searchInfo", equipment);
        model.addAttribute("pageInfo", new PageInfo(pageInfo));
        return prefix+"apply/applyEquipment";
    }

    /**
     * 添加跳转页面
     * @return
     */
    @GetMapping("applyBefore/{id}")
    public String applyBefore(Model model,@PathVariable("id") Long id){
        model.addAttribute("equipment",equipmentService.getById(id));
        String  sysUser = Utility.getCurrentUsername();
        model.addAttribute("account", sysUser);

        return prefix+"apply/applys";
    }
    /**
     * 申请新设备
     * @param equipment
     * @return
     */
    @PostMapping("applys")
    @ResponseBody
    public AjaxResult applys(Equapply equipment){
        Equipment  equipment1 = equipmentService.selectByPrimaryKey(equipment.getEquId());
        equipment1.setEquStatus("申请中");
        equipmentService.updateById(equipment1);

        return toAjax(equapplyService.save(equipment));
    }


    @RequestMapping("adminEquipment")
    public String adminEquipment(Model model,@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, Equipment equipment) {
        Page<Equipment> page = new Page<Equipment>(pageNo, pageSize);
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<Equipment>();
        if(!ObjectUtils.isEmpty(equipment.getEquId())) {
            queryWrapper = queryWrapper.like("equ_id",equipment.getEquId());
        }
        if(!ObjectUtils.isEmpty(equipment.getEquName())) {
            queryWrapper = queryWrapper.like("equ_name",equipment.getEquName());
        }
        if(!ObjectUtils.isEmpty(equipment.getEquType())) {
            queryWrapper = queryWrapper.like("equ_type",equipment.getEquType());
        }
        if(!ObjectUtils.isEmpty(equipment.getEquSizes())) {
            queryWrapper = queryWrapper.like("equ_sizes",equipment.getEquSizes());
        }
        if(!ObjectUtils.isEmpty(equipment.getEquStatus())) {
            queryWrapper = queryWrapper.like("equ_status",equipment.getEquStatus());
        }

        queryWrapper.eq("equ_status", "申请中");
        IPage<Equipment> pageInfo = equipmentService.page(page, queryWrapper);
        model.addAttribute("searchInfo", equipment);
        model.addAttribute("pageInfo", new PageInfo(pageInfo));
        return prefix+"admin/list";
    }

    /**
     * 管理员设备维修申请
     * @param model
     * @param pageNo
     * @param pageSize
     * @param equipment
     * @return
     */
    @RequestMapping("lists")
    public String lists(Model model,@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, Equipment equipment) {
        Page<Equipment> page = new Page<Equipment>(pageNo, pageSize);
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<Equipment>();
        if(!ObjectUtils.isEmpty(equipment.getEquId())) {
            queryWrapper = queryWrapper.like("equ_id",equipment.getEquId());
        }
        if(!ObjectUtils.isEmpty(equipment.getEquName())) {
            queryWrapper = queryWrapper.like("equ_name",equipment.getEquName());
        }
        if(!ObjectUtils.isEmpty(equipment.getEquType())) {
            queryWrapper = queryWrapper.like("equ_type",equipment.getEquType());
        }
        if(!ObjectUtils.isEmpty(equipment.getEquSizes())) {
            queryWrapper = queryWrapper.like("equ_sizes",equipment.getEquSizes());
        }
        IPage<Equipment> pageInfo = equipmentService.page(page, queryWrapper);
        model.addAttribute("searchInfo", equipment);
        model.addAttribute("pageInfo", new PageInfo(pageInfo));
        return prefix+"lists";
    }
}

